package com.yanqu.road.server.cmd.exchange;

import com.yanqu.road.logic.bussiness.servercenter.ExchangeBussiness;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.servercenter.ExchangeInfo;
import com.yanqu.road.pb.server.ServerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.exchange.ExchangeModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_USE_EXCHANGE_KEY, desc = "使用兑换码")
public class UseExchangeKeyCmd extends AbstractCommand {

    private String key;

    private String reward;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ServerProto.UseExchangeKeyReqMsg reqMsg = ServerProto.UseExchangeKeyReqMsg.parseFrom(packet.getBytes());
        key = reqMsg.getKey();
        return true;
    }

    //TODO server
    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ExchangeModule exchangeModule = player.getModule(ExchangeModule.class);
        if (!exchangeModule.isLoad()) {
            ret = GameErrorCode.E_EXCHANGE_CODE_NOT_INIT;
            return;
        }
        if(exchangeModule.isUseExchangeKey(key)){
            ret = GameErrorCode.E_EXCHANGE_KEY_HAS_USE;
            return;
        }
        ExchangeInfo exchangeInfo = ExchangeBussiness.getExchangeInfo(key);
        if(null == exchangeInfo){
            ret = GameErrorCode.E_EXCHANGE_KEY_NO_FOUND;
            return;
        }
        if(exchangeInfo.isDelete()){
            ret = GameErrorCode.E_EXCHANGE_KEY_INVALID;
            return;
        }
        if (exchangeInfo.getType() != 0 && exchangeInfo.getUseTimes() > 0) {//已经被使用了
            ret = GameErrorCode.E_EXCHANGE_KEY_INVALID;
            return;
        }
        int channelId = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId()).getChannelId();
        if (exchangeInfo.getChannelId() > 0 && exchangeInfo.getChannelId() != channelId) {// 是否是专服礼包
            ret = GameErrorCode.E_EXCHANGE_KEY_INVALID;
            return;
        }
        long serverId = GameServer.getInstance().getServerId();
        if (exchangeInfo.getServerId() > 0 && exchangeInfo.getServerId() != serverId) {// 是否是对应的服务器
            ret = GameErrorCode.E_EXCHANGE_KEY_INVALID;
            return;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        if (nowTime < exchangeInfo.getStartTime() || nowTime > exchangeInfo.getEndTime()) {//超过时间了
            ret = GameErrorCode.E_EXCHANGE_KEY_INVALID;
            return;
        }

        player.getModule(ExchangeModule.class).addUserExchange(key);
        ExchangeBussiness.updateExchangeUseTimes(key);

        player.getModule(CurrencyModule.class).addCurrency(exchangeInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.ExchangeReward);
        reward = exchangeInfo.getReward();
    }

    @Override
    public void sendData(GamePlayer player) {
        ServerProto.UseExchangeKeyRespMsg.Builder respMsg = ServerProto.UseExchangeKeyRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (ret == 0) {
            respMsg.setReward(reward);
        }
        player.sendPacket(Protocol.U_USE_EXCHANGE_KEY, respMsg);
    }

}
